package main

import (
	"fmt"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/redis"
	"github.com/gin-gonic/gin"
)

func main() {
	router := gin.Default()
	// 初始化容器
	store, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret"))
	router.Use(sessions.Sessions("mysession", store))

	router.GET("/test", func(context *gin.Context) {
		// 调用session, 设置session数据
		session := sessions.Default(context)
		//// 设置session
		//session.Set("name", "CC")
		//// 修改session时，需要save函数配合，否则不生效
		//session.Save()
		val := session.Get("name")
		fmt.Println("获取session： ", val.(string))
		context.Writer.WriteString("测试 Sessions ...")
		// 设置cookie
		//context.SetCookie("mytest", "CC test", 60*60, "", "", false, true)
		//val, _ := context.Cookie("mytest")
		//fmt.Println("获取的Cookie为： ", val)
		//context.Writer.WriteString("测试 Cookie...")
	})

	router.Run(":9999")
}
